package model

import (
	"choujiang/common/db"
	"fmt"
)

type UserGameOpenid struct {
	Id          int64  `xorm:"INT(11) not null pk autoincr"`
	UserId      int64  `xorm:"INT(11) not null default 0"`
	GameId      string `xorm:"not null default '' VARCHAR(255) index"`
	MiniOpenId  string `xorm:"not null default '' VARCHAR(255) index unique"`
	MiniUnionId string `xorm:"not null default '' VARCHAR(50) index"`
	QrPath      string `xorm:"VARCHAR(255)"`
	CreatedAt   int64  `xorm:"not null default 0 INT(11) created"`
	UpdatedAt   int64  `xorm:"not null default 0 INT(11) updated"`
}

func GetUserGameOpenid(gameId, openId string) *UserGameOpenid {
	ugo := new(UserGameOpenid)
	has, err := db.MysqlEngine.Where("mini_open_id=? and game_id=?", openId, gameId).Get(ugo)
	fmt.Printf("GetUserGameOpenid err %+v\n", err)
	if !has || err != nil {
		return nil
	}
	return ugo
}

func GetUgoByGameIdUserId(userId int64, gameId string) *UserGameOpenid {
	ugo := new(UserGameOpenid)
	has, err := db.MysqlEngine.Where("game_id=? and user_id=?", gameId, userId).Get(ugo)
	fmt.Printf("GetUserGameOpenid err %+v\n", err)
	if !has || err != nil {
		return nil
	}
	return ugo
}

func (u *UserGameOpenid) Insert() (*UserGameOpenid, error) {
	_, err := db.MysqlEngine.InsertOne(u)
	return u, err
}

func (u *UserGameOpenid) Update() (*UserGameOpenid, error) {
	_, err := db.MysqlEngine.ID(u.Id).Update(u)
	return u, err
}
